package com.example.baidumap_a.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.SparseArray;
import android.widget.RadioGroup;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import com.example.baidumap_a.fragment.MyFragment;
import com.example.baidumap_a.fragment.OneFragment;
import com.example.baidumap_a.fragment.orderFragment;
import com.example.baidumap_a.R;

public class MainActivity extends AppCompatActivity implements OneFragment.OneListener, MyFragment.MyFgListener,orderFragment.orderFgListener {
    private RadioGroup radioGroup;
    private TextView tv_title;
    private SparseArray<Fragment> mFragmentSparseArray;
    private String name;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent=getIntent();
        name=intent.getStringExtra("username");
        initView();

    }

    private void initView() {
        tv_title = findViewById(R.id.text_Title_main);
        radioGroup = findViewById(R.id.tabs_rg);
        mFragmentSparseArray = new SparseArray<>();
        mFragmentSparseArray.append(R.id.One_tab, new OneFragment());
        mFragmentSparseArray.append(R.id.order_tab, new orderFragment());
        mFragmentSparseArray.append(R.id.My_tab, new MyFragment());

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // 具体的fragment切换逻辑可以根据应用调整，例如使用show()/hide()
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        mFragmentSparseArray.get(checkedId)).commit();
                switch (checkedId) {
                    case R.id.One_tab:
                        tv_title.setText("首页");
                        break;
                    case R.id.order_tab:
                        tv_title.setText("订单");
                        break;
                    case R.id.My_tab:
                        tv_title.setText("我的");
                        break;
                }
            }
        });
        // 默认显示第一个
        getSupportFragmentManager().beginTransaction().add(R.id.fragment_container,
                mFragmentSparseArray.get(R.id.One_tab)).commit();
    }

//OneFragment回调接口
    @Override
    public String OneGetUsername() {
        return name;
    }
//MyFragment回调接口
    @Override
    public String MyFgGetUsername() {
        return name;
    }
    //orderFragment回调接口
    @Override
    public String orderGetUsername() {
        return name;
    }
}
